import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:useable_app/base/view.dart';
import 'package:useable_app/viewmodel/ThemeViewModel.dart';
import '../global/GlobalTheme.dart';

class ThemeSetting extends StatefulWidget {
  const ThemeSetting({Key? key}) : super(key: key);

  @override
  _ThemeSettingState createState() => _ThemeSettingState();
}

class _ThemeSettingState extends State<ThemeSetting> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: getAppbar('主题设置'),
        body: ListView.builder(
          itemBuilder: _itemBuilder,
          itemCount: themes.length,
        ));
  }
}

Widget _itemBuilder(BuildContext context, int index) {
  return Padding(
    padding: EdgeInsets.all(10),
    child: GestureDetector(
      child: Container(
        width: double.infinity,
        height: 50,
        decoration: BoxDecoration(
            color: themes[index],
            borderRadius: BorderRadius.all(Radius.circular(10))),
        child: Provider.of<ThemeViewModel>(context).getColor == index
            ? Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Icon(
                    Icons.check,
                    color: Colors.white,
                  ),
                  SizedBox(
                    width: 18,
                  )
                ],
              )
            : Row(),
      ),
      onTap: () async{
        // 持久化保存主题颜色
        SharedPreferences sp = await SharedPreferences.getInstance();
        sp.setInt('color', index);
        context.read<ThemeViewModel>().setColor(index);
      },
    ),
  );
}
